<template>
  <div>
    <h2>注册</h2>
    <table class="table table-bordered">
      <tr>
        <td>用户名：</td>
        <td><input type="text" v-model="state.U_Account" class="form-control" ></td>
      </tr>
      <tr>
        <td>密码：</td>
        <td><input type="password"  v-model="state.U_Pwd" class="form-control"></td>
      </tr>
      <tr>
        <td colspan="2">
          <button @click="register" class="btn btn-primary">注册</button>
        </td>
      </tr>
    </table>
  </div>
</template>
<script setup lang="ts">
import {reactive,toRefs}from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
const router = useRouter()

const data=reactive({
  state:{
    U_Account:'',
    U_Pwd:''
  }
})

const {state} =toRefs(data)



const register=()=>{
  
  if(state.value.U_Account==""){
    alert("用户账号不能为空")
    return
  }
  if(state.value.U_Pwd==""){
    alert("用户密码不能为空")
    return
  }
  axios({
    url:'http://localhost:54566/api/User/PostUserResiger',
    method:'post',
    data:state.value
  })
  .then(res=>{
    console.log(res.data)
    if(res.data==-1){
    alert("用户账号不能重复")
    return
    }
    if(res.data>0){
      alert("注册成功")
      router.push("/LoginView")
    }else{
      alert("注册失败")
      return
    }
  })
  .catch(err=>{
    console.log(err)
  })
}

</script>